/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wii.elpro.dao.jpa;

import com.wii.elpro.dao.JPAUtil;
import com.wii.elpro.dao.PriceDAOInterface;
import com.wii.elpro.entity.Jenis;
import com.wii.elpro.entity.Price;
import com.wii.elpro.entity.Unit;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author hendro
 */
public class PriceDAOImpl extends DAOGenericImpl implements PriceDAOInterface {

    public Price getPrice(long id) throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.find(Price.class, id);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Price> getPrices() throws Exception {
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            return em.createQuery("SELECT d FROM Price d").getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Price> getPricesByUnit(Unit unit) throws Exception {
        List<Price> prices = new ArrayList<Price>();
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            prices = em.createQuery("SELECT prc FROM Price prc WHERE " +
                    "prc.unit=:input1").setParameter("input1", unit).getResultList();
        } catch (Exception e) {
            throw e;
        }
        return prices;
    }

    public List<Price> getPricesByJenis(Jenis jenis) throws Exception {
        List<Price> prices = new ArrayList<Price>();
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            prices = em.createQuery("SELECT prc FROM Price prc WHERE " +
                    "prc.jenis=:input1").setParameter("input1", jenis).getResultList();
        } catch (Exception e) {
            throw e;
        }
        return prices;
    }

    public List<Price> getPricesByUnitAndJenis(Unit unit, Jenis jenis) throws Exception {
        List<Price> prices = new ArrayList<Price>();
        JPAUtil.beginTransaction();
        EntityManager em = JPAUtil.getEntityManager();
        try {
            prices = em.createQuery("SELECT prc FROM Price prc WHERE " +
                    "prc.unit=:input1 AND prc.jenis=:input2").setParameter("input1", unit).setParameter("input2", jenis).getResultList();
        } catch (Exception e) {
            throw e;
        }
        return prices;
    }
}
